<template>
  <su-page>
    <su-body padding="0 15px">
      <u--form labelPosition="top" :model="formData" :rules="rules" ref="form1">
        <!-- <u-form-item labelWidth="100px" label="工程名称" prop="title" borderBottom ref="item1">
          <u--input v-model="formData.title" placeholder="请输入工程名称" border="none"></u--input>
        </u-form-item> -->
        <u-form-item labelWidth="100px" label="形象进度" prop="jindu" borderBottom ref="item1">
          <u--input v-model="formData.jindu" placeholder="请输入形象进度" border="none"></u--input>
        </u-form-item>
		<u-form-item labelWidth="100px" label="年度" prop="nd" borderBottom ref="item1">
		   <su-select :options="ndOptions" placeholder="请选择年度" v-model="formData.nd"></su-select>
		</u-form-item>
		<u-form-item labelWidth="100px" label="月份" prop="yf" borderBottom ref="item1">
		 <su-select :options="yfOptions" placeholder="请选择年度" v-model="formData.yf"></su-select>
		</u-form-item>
        <u-form-item labelWidth="100px" label="本月完成产值" prop="wccz" borderBottom ref="item1">
          <u--input v-model="formData.wccz" placeholder="请输入本月完成产值" border="none"></u--input>
        </u-form-item>
        <u-form-item labelWidth="100px" label="本月实现税收" prop="sxss" borderBottom ref="item1">
          <u--input v-model="formData.sxss" placeholder="请输入本月实现税收" border="none"></u--input>
        </u-form-item>
		<u-form-item labelWidth="100px" label="本月工程进度款" prop="gcjdk" borderBottom ref="item1">
		  <u--input v-model="formData.gcjdk" placeholder="请输入本月工程进度款" border="none"></u--input>
		</u-form-item>
        <u-form-item labelWidth="100px" label="填报人" prop="tbr" borderBottom ref="item1">
          <u--input v-model="formData.tbr" placeholder="请输入填报人" border="none"></u--input>
        </u-form-item>
		<u-form-item labelWidth="100px" label="填报人联系方式" prop="tbrlxfs" borderBottom ref="item1">
		  <u--input v-model="formData.tbrlxfs" placeholder="请输入填报人联系方式" border="none"></u--input>
		</u-form-item>
        <!-- <u-form-item labelWidth="180px" label="项目经理联系方式" prop="phone" borderBottom ref="item1">
          <u--input v-model="formData.phone" placeholder="请输入项目经理联系方式" border="none"></u--input>
        </u-form-item>
        <u-form-item labelWidth="100px" label="项目监理" prop="xmjl" borderBottom ref="item1">
          <su-select :options="sprOptions" placeholder="请选择项目监理" v-model="formData.xmjl"></su-select>
        </u-form-item>
		<u-form-item labelWidth="100px" label="填报时间" prop="spr" borderBottom ref="item1">
		 <u--input v-model="formData.danwei" placeholder="请输入总包单位" border="none"></u--input>
		</u-form-item>
        <u-form-item labelWidth="180px" label="填报人" prop="xmjl" borderBottom ref="item1">
          <u--input v-model="formData.xmjlphone" placeholder="请选择项目监理联系方式" border="none"></u--input>
        </u-form-item> -->
        <u-form-item labelWidth="100px" label="现场照片" prop="files" borderBottom ref="item1">
               <u-upload
               	:fileList="formData.files"
               	@afterRead="afterRead"
               	@delete="deletePic"
               	name="3"
               	multiple
               	:maxCount="10"
               	:previewFullImage="true"
               ></u-upload>
        </u-form-item>
		
		<u-form-item labelWidth="100px" label="税票" prop="file" borderBottom ref="item1">
               <u-upload
               	:fileList="formData.file"
               	@afterRead="afterReadsp"
               	@delete="deletePicsp"
               	name="3"
               	multiple
               	:maxCount="10"
               	:previewFullImage="true"
               ></u-upload>
		</u-form-item>
		
      </u--form>
      <su-fixed-footer transparent>
        <u-button type="default" size="large" :loading="loading" text="取消" @click="goBack"></u-button>
        <u-button type="primary" size="large" :loading="loading" text="提交" @click="submitForm(formData)"></u-button>
      </su-fixed-footer>
    </su-body>
  </su-page>
</template>

<script>
  /**
   * @description 发起流程
   * @date 2022-03-15
   * @version v1.0.1
   * @author gf
   */
  import {
    register
  } from '@/common/api.js'
  import {runSQL} from '../../../../common/utils/wxutils.js'
  let rid;
  export default {
    data() {
      return {
        loading: false,
        ndOptions: [{
            label: '2021',
            value: '2021'
          },
          {
            label: '2022',
            value: '2022'
          },
          {
            label: '2023',
            value: '2023'
          },
          {
            label: '2024',
            value: '2024'
          },
          {
            label: '2025',
            value: '2025'
          },
          {
            label: '2026',
            value: '2026'
          },
		  {
		    label: '2027',
		    value: '2027'
		  },
		  {
		    label: '2028',
		    value: '2028'
		  }, 
		  {
		    label: '2029',
		    value: '2029'
		  },
		  {
		    label: '2030',
		    value: '2030'
		  },
        ],
		yfOptions: [
			{label: '1',value: '1'},
			{label: '2',value: '2'},
			{label: '3',value: '3'},
			{label: '4',value: '4'},
			{label: '5',value: '5'},
			{label: '6',value: '6'},
			{label: '7',value: '7'},
			{label: '8',value: '8'},
			{label: '9',value: '9'},
			{label: '10',value: '10'},
			{label: '11',value: '11'},
			{label: '12',value: '12'},
		  ],
        // form表单数据对象	
        formData: {
		  jindu:'',
          nd: '',
          yf: '',
          wccz: '',
          sxss:'',
		  gcjdk:'',
		  tbr: '',
		  tbrlxfs:'',
          files:[],
		  file:[],
		  fjid:[],
		  fjids:[],
        },
        // 表单校验规则
        rules: {
           jindu: [{
             required: true,
             message: '请填写形象进度',
             trigger: ['change', 'blur'],
           }],
           nd: {
             required: true,
             validator: (rule, value, callback) => {
               return value !== '';
             },
             message: '请填写年度',
             trigger: ['change'],
			 type: 'number',
           },
		   yf: {
		     required: true,
		     validator: (rule, value, callback) => {
		       return value !== '';
		     },
		     message: '请填写月份',
		     trigger: ['change'],
			 type: 'number',
		   },
        }
      }
    },
    onLoad(options) {
		rid=options.id;
      this.$nextTick(()=>{
        this.$refs.form1.setRules(this.rules)
      })
    },
	
    methods: {
		async afterRead(event) {
			let lists = [].concat(event.file)
			let fileListLen = this.formData.files.length
			lists.map((item) => {
				this.formData.files.push({
					...item,
					status: 'uploading',
					message: '上传中'
				})
			})
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].url);
				// this.insertImg(result);
				let item = this.formData.files[fileListLen]
				this.formData.files.splice(fileListLen, 1, Object.assign(item, {
					status: 'success',
					message: '',
					data:JSON.parse(result.data)
					// url: result
				}))
				fileListLen++
			}
		},
		uploadFilePromise(url) {
			let vthis = this;
			return new Promise((resolve, reject) => {
				let a = uni.uploadFile({
					url: 'http://120.221.95.208:8083/HdzjAppport/uploadb', // 仅为示例，非真实的接口地址`${zhhfurl}zhhf-info/WeChat/getOpenId`,
					filePath: url,
					name: 'file',
					formData: {
						recid: rid
					},
					success: (res) => {
						let obj = JSON.parse(res.data)
							let oo = JSON.stringify(obj)
						setTimeout(() => {
							vthis.formData.fjid.push(
								obj.fileid
							)
							resolve(res);
						}, 1000)
					},
						fail(err) {
							console.log(err)
						}
				});
			})
		},
		deletePic(event) {
			this.formData.files.splice(event.index, 1)
			this.formData.fjid.splice(event.index, 1)
		},
		//税票
		async afterReadsp(event) {
			let lists = [].concat(event.file)
			let fileListLen = this.formData.file.length
			lists.map((item) => {
				this.formData.file.push({
					...item,
					status: 'uploading',
					message: '上传中'
				})
			})
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromisesp(lists[i].url);
				// this.insertImg(result);
				let item = this.formData.file[fileListLen]
				this.formData.file.splice(fileListLen, 1, Object.assign(item, {
					status: 'success',
					message: '',
					data:JSON.parse(result.data)
					// url: result
				}))
				fileListLen++
			}
		},
		uploadFilePromisesp(url) {
			let vthis = this;
			return new Promise((resolve, reject) => {
				let a = uni.uploadFile({
					url: 'http://120.221.95.208:8083/HdzjAppport/uploadb', // 仅为示例，非真实的接口地址`${zhhfurl}zhhf-info/WeChat/getOpenId`,
					filePath: url,
					name: 'file',
					formData: {
						recid: rid
					},
					success: (res) => {
						console.log(res);
						let obj = JSON.parse(res.data)
						console.log(obj)
							let oo = JSON.stringify(obj)
							console.log(oo)
						setTimeout(() => {
							vthis.formData.fjids.push(
								obj.fileid
							)
							resolve(res);
						}, 1000)
					},
						fail(err) {
							console.log(err)
						}
				});
			})
		},
		deletePicsp(event) {
			this.formData.file.splice(event.index, 1)
			this.formData.fjids.splice(event.index, 1)
		},
      goBack(){
		  
        this.$u.route({
          type:'back'
        })
      },
      // 提交表单
      submitForm() {
		  runSQL("select * from GCJDMYXXB where gcbh=(select gcbh from ssnydw.gcbdxxb where recid="+rid+");").then(res=>{
	
			   runSQL("select * from MYJDXXFB where recid="+res[0][0].RECID+" and nd='"+this.formData.nd+"' and yf="+this.formData.yf+" and sfqr='已提交';").then(ress=>{
		 
		  if(ress[0].length>0){
			  this.$u.toast('该月已填报并且提交给了企业审核！')
		  }else{
			  
			 runSQL("select sum(byysxss)byysxss,sum(byywccz)byywccz from MYJDXXFB where  recid="+res[0][0].RECID+"and nd='"+this.formData.nd+"' and yf<="+this.formData.yf+";").then(resa=>{
			    let ljcz=0;
				let ljss=0;
				if(resa[0][0].BYYWCCZ==""||resa[0][0].BYYWCCZ==null){
					ljcz=this.formData.wccz;
				}else{
					ljcz=resa[0][0].BYYWCCZ+this.formData.wccz;
				}
				if(resa[0][0].BYYSXSS==""||resa[0][0].BYYSXSS==null){
					ljss=this.formData.sxss;
				}else{
					ljss=resa[0][0].BYYSXSS+this.formData.sxss;
				}
				
				
				let xczp="";
				let spzp="";
				for(let i=0;i<this.formData.fjid.length;i++){
					if(i==0){
						xczp=this.formData.fjid[i];
					}else{
						xczp=xczp+","+this.formData.fjid[i];
					}
					
				}
				
				for(let j=0;j<this.formData.fjids.length;j++){
					if(j==0){
						spzp=this.formData.fjids[j];
					}else{
						spzp=spzp+","+this.formData.fjids[j];
					}
				}
				 runSQL("insert into MYJDXXFB (recid,xxjd,byywccz,byysxss,bygcjdk,ljwccz,ljsxss,tbsj,jdtbr,fjid,createdate,yf,nd,sftj,sfqr,spfp)values("+res[0][0].RECID+",'"+this.formData.jindu+"',"+this.formData.wccz+","+this.formData.sxss+","+this.formData.gcjdk+","+ljcz+","+ljss+",now(),'"+this.formData.tbr+"','"+xczp+"',now(),"+this.formData.yf+",'"+this.formData.nd+"','审核','已提交','"+spzp+"');").then(resb=>{
					 if(resb[0]==1){
						 if(this.formData.files.length>0){
							
							 for(let a=0;a<this.formData.files.length;a++){
								let fhsj=this.formData.files[a].data;
								let fjpn=fhsj.url.split("D://SSNYDWFile")[1];
								let fjpth=fjpn.split(fhsj.wjm)[0]; 
								runSQL("insert into tbfile (fileid,filename,filepath,filesize,lastupdate,storefilename,serverid,disporder,version)values('"+fhsj.fileid+"','"+fhsj.wjm+"','"+fjpth+"',1024,now(),'"+fhsj.wjm+"',1,"+rid+",1);").then(resc=>{
												
												
												 })						 
							} 
						 }
						 //税票
						 if(this.formData.file.length>0){
						 		for(let a=0;a<this.formData.file.length;a++){
						 		let fhsj=this.formData.file[a].data;
						 		let fjpn=fhsj.url.split("D://SSNYDWFile")[1];
						 		let fjpth=fjpn.split(fhsj.wjm)[0]; 
						 runSQL("insert into tbfile (fileid,filename,filepath,filesize,lastupdate,storefilename,serverid,disporder,version)values('"+fhsj.fileid+"','"+fhsj.wjm+"','"+fjpth+"',1024,now(),'"+fhsj.wjm+"',1,"+rid+",1);").then(resc=>{
						 												
						 												 })						 
						 } 
						 }
	
					      setTimeout(() => {
					        this.loading = false
					        this.$u.toast('提交成功')
					        this.$u.route({
					          type: 'back'
					        })
					      }, 500)
					
					 
					 
					 }else{
						 uni.$u.toast('提交出错');
					 }
					 })
				 })
		  }
		  })
		  })
        // this.$refs.form1.validate().then(ok => {
        //   console.log(ok)
        //   if (ok) {
        //     this.loading = true
        //     setTimeout(() => {
        //       this.loading = false
        //       this.$u.toast('保存成功')
        //       this.$u.route({
        //         type: 'back'
        //       })
        //     }, 500)
        //   }
        // }).catch(err => {
        //   return
        // })
      },
	  handleSuccess(res, file) {
	        this.$u.toast('保存成功')
	     },
	handleError(){
		this.$u.toast('保存成功')
	}
    },
	
	
  }
</script>

<style lang="scss">
</style>
